package com.ruoyi.web.controller.tech;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.model.ProductUpdateStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Technicians;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ITechniciansService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Anonymous  //此注解是不加权限直接可以调用该类的所有接口
@RestController
@CrossOrigin //这里是解决前端跨域的问题
@RequestMapping("/company/technicians")
public class TechniciansController extends BaseController {

    @Autowired
    private ITechniciansService techniciansService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    //列表
    //http://127.0.0.1:9070/company/technicians/list
    @GetMapping("/list")
    public TableDataInfo list(Technicians technicians) {
        startPage();
        List<Technicians> list = techniciansService.selectList(technicians);
        return getDataTable(list);
    }

    //删除人员
    //http://127.0.0.1:9070/company/technicians/11
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        techniciansService.deleteTechnicianByIds(ids);
        return success();
    }

    //修改人员状态
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@Validated @RequestBody Technicians technician) {
        int i = techniciansService.updateStatus(technician);
        return toAjax(i);
    }

    //批量修改人员状态上架下架
    @PostMapping("/batchUpdateStatus")
    public AjaxResult batchUpdateStatus(@RequestBody ProductUpdateStatus productUpdateStatus) {
        List<Long> ids = productUpdateStatus.getIds();
        Technicians technician = techniciansService.selectById(ids.get(0));
        if (StringUtils.isEmpty(ids)) {
            return error("待更新状态为空");
        }
        List<SysDictData> sysDictDatas = sysDictTypeService.selectDictDataByType("business_status");
        if (sysDictDatas.stream().noneMatch(sysDictData -> Objects.equals(sysDictData.getDictValue(), productUpdateStatus.getStatus()))) {
            return error(String.format("状态值:{}非法", productUpdateStatus.getStatus()));
        }
        int i = techniciansService.batchUpdateStatus(productUpdateStatus);
        return success(StringUtils.format("成功更新{}条数据", i));
    }

    //根据id获取详细信息
    //http://127.0.0.1:9070/company/technicians/1
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return success(techniciansService.selectById(id));
    }

    //新增人员
    //http://127.0.0.1:9070/company/technicians/add
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody Technicians technician) {
        return toAjax(techniciansService.insertTechnicians(technician));
    }

    //修改人员
    //http://127.0.0.1:9070/company/technicians/update
    @PostMapping("/update")
    public AjaxResult update(@Validated @RequestBody Technicians technicians) {
        return toAjax(techniciansService.updateTechnicians(technicians));
    }

}
